//
//  LoginViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "RegisterViewController.h"
#import <AVOSCloud.h>
#import "ForgetPasswordViewController.h"
#import <AVOSCloudSNS.h>


@interface LoginViewController ()



@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
//    [AVOSCloudSNS setupPlatform:AVOSCloudSNSSinaWeibo withAppKey:@"3218984614" andAppSecret:@"fa6913aea70c25daea72c54a38874cd1" andRedirectURI:@"http://wanpaiapp.com/oauth/callback/sina"];
    
   
    
    UIImageView *left = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-fangqiuzhiyepeiyangzhuanhuan"]];
    self.UesrName.leftView = left;
    self.UesrName.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *left2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-mima"]];
    self.passWord.leftView = left2;
    self.passWord.leftViewMode = UITextFieldViewModeAlways;
    
    
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 5;
    
    self.title =@"登陆";
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStylePlain) target:self action:@selector(Rebish:)];
    
}

-(void)Rebish:(UIBarButtonItem*)button
{
    RegisterViewController * reVC =[[RegisterViewController alloc] init];
    reVC.myNameBlock = ^(NSString *name){
        self.UesrName.text = name;
        
    };
    
   [self.navigationController pushViewController:reVC animated:YES];
}

#pragma mark --找回密码
- (IBAction)Zhaohui:(id)sender {
    ForgetPasswordViewController *forget = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forget animated:YES];
}

- (IBAction)LoginButton:(id)sender {
    [AVUser logInWithUsernameInBackground:_UesrName.text password:_passWord.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else{
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码或用户名不正确" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];

            
        }
        
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
